package com.jgp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jgp.common.R;
import com.jgp.entity.Category;
import com.jgp.service.CateGoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
/**
 * 分类管理
 */
public class CategoryController {
    @Autowired
    private CateGoryService cateGoryService;
    @PostMapping
    public R<String> save(@RequestBody Category category){
        cateGoryService.save(category);
        return R.success("新增分类成功");
}
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize ){
        log.info("page={},pageSize={},name={}",page,pageSize);
        //    构造分页构造器
        Page <Category> pageInfo1 = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        //    过滤条件

        //排序条件
        queryWrapper.orderByAsc(Category::getSort);
        //    执行查询
        cateGoryService.page(pageInfo1,queryWrapper);
        return R.success(pageInfo1);
    }

    /**
     * 根据id删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除分类id:{}", ids);
//        cateGoryService.removeById(ids);
        cateGoryService.remove(ids);
        return R.success("分类信息删除成功");
    }
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);
        cateGoryService.updateById(category);
        return R.success("修改分类信息成功");
    }
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(category.getType()!=null,Category :: getType,category.getType());
        QueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = cateGoryService.list(QueryWrapper);
        return  R.success(list);

    }

}
